<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Save the manuall given ordernumbers
 * used from article.page.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2015
 * @author     Norbert Gebert
 * @license    GPL3
 */
$orderNumber = $_POST['orderm'];
$orderId = $_POST['orderId'];
$ok = FALSE;
$error = FALSE;

for ($a = 0; $a < count($orderId); $a++)
{ // Test entrys
	$orderNr = intval($orderNumber[$a]);
	if ($refValidate->testNumber($orderNr,
			_('Entry are wrong for Id') . ': ' . $orderId[$a]) == FALSE)
	{
		message::setErrorRow(_('Failed to save the new order!'), TRUE);
		$ok = FALSE;
		break;
	}
	else
		$ok = TRUE;
}

if ($ok == TRUE)
{ // Save entrys
	for ($a = 0; $a < count($orderId); $a++)
	{
		//echo $orderNumber[$a];
		$data = Array('product_order' => $orderNumber[$a]);
		db::getInstance()->where('product_id', $orderId[$a]);

		if (db::getInstance()->update('product', $data) == false)
		{
			message::setErrorRow(_('Failed to save the order!') .
				' (Id= ' . $orderId[$a] . ')', FALSE);
			$error = TRUE;
			break;
		}
	}
	if ($error == FALSE)
		message::setNoteRow(_('Changes saved successfully.'), TRUE);
}
